package com.floreantpos.bo.ui.explorer;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import com.floreantpos.main.Application;
import com.floreantpos.model.Guest;
import com.floreantpos.model.dao.GuestDAO;
import com.floreantpos.swing.MessageDialog;
import com.floreantpos.swing.TransparentPanel;
import com.floreantpos.ui.PosTableRenderer;
import com.floreantpos.ui.dialog.BeanEditorDialog;
import com.floreantpos.ui.dialog.ConfirmDeleteDialog;
import com.floreantpos.ui.model.GuestForm;

public class GuestExplorer extends TransparentPanel {

  private List<Guest> guestList;

  private JTable table;

  private GuestExplorerTableModel tableModel;

  public GuestExplorer() {
    GuestDAO dao = new GuestDAO();
    guestList = dao.findAll();

    tableModel = new GuestExplorerTableModel();
    table = new JTable(tableModel);
    table.setAutoCreateRowSorter(true);
    table.setDefaultRenderer(Object.class, new PosTableRenderer());

    setLayout(new BorderLayout(5, 5));
    add(new JScrollPane(table));

    JButton addButton = new JButton(com.floreantpos.POSConstants.ADD);
    addButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          GuestForm editor = new GuestForm();
          BeanEditorDialog dialog = new BeanEditorDialog(editor, Application.getInstance().getBackOfficeWindow(), true);
          dialog.open();
          if (dialog.isCanceled()) {
            return;
          }
          Guest guest = (Guest) editor.getBean();
          tableModel.addGuest(guest);
        } catch (Exception x) {
          MessageDialog.showError(com.floreantpos.POSConstants.ERROR_MESSAGE, x);
        }
      }

    });

    JButton editButton = new JButton(com.floreantpos.POSConstants.EDIT);
    editButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          int index = table.convertRowIndexToModel(table.getSelectedRow());
          if (index < 0) {
            return;
          }

          Guest guest = guestList.get(index);

          GuestForm editor = new GuestForm(guest);
          BeanEditorDialog dialog = new BeanEditorDialog(editor, Application.getInstance().getBackOfficeWindow(), true);
          dialog.open();
          if (dialog.isCanceled()) {
            return;
          }

          table.repaint();
        } catch (Throwable x) {
          MessageDialog.showError(com.floreantpos.POSConstants.ERROR_MESSAGE, x);
        }
      }

    });
    JButton deleteButton = new JButton(com.floreantpos.POSConstants.DELETE);
    deleteButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          int index = table.convertRowIndexToModel(table.getSelectedRow());
          if (index < 0) {
            return;
          }

          if (ConfirmDeleteDialog.showMessage(GuestExplorer.this, com.floreantpos.POSConstants.CONFIRM_DELETE, com.floreantpos.POSConstants.DELETE) == ConfirmDeleteDialog.YES) {
            Guest guest = guestList.get(index);
            if (guest.getCurrentBalance() == 0) {
              GuestDAO dao = new GuestDAO();
              dao.delete(guest);
              tableModel.deleteGuest(guest, index);
            } else {
              MessageDialog.showError(com.floreantpos.POSConstants.CANNOT_DELETE_CUSTOMERS_WITH_OPEN_BALANCE);
            }

          }
        } catch (Exception x) {
          MessageDialog.showError(com.floreantpos.POSConstants.ERROR_MESSAGE, x);
        }
      }

    });

    TransparentPanel panel = new TransparentPanel();
    panel.add(addButton);
    panel.add(editButton);
    panel.add(deleteButton);
    add(panel, BorderLayout.SOUTH);
  }

  class GuestExplorerTableModel extends AbstractTableModel {

    String[] columnNames = {com.floreantpos.POSConstants.ID, com.floreantpos.POSConstants.NAME, "Documento", com.floreantpos.POSConstants.TELEPHONE, "Endereço 1", "Taxa de Entrega", "Saldo Atual"};

    public int getRowCount() {
      if (guestList == null) {
        return 0;
      }
      return guestList.size();
    }

    public int getColumnCount() {
      return columnNames.length;
    }

    @Override
    public String getColumnName(int column) {
      return columnNames[column];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
      return false;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
      if (guestList == null) {
        return "";
      }

      Guest guest = guestList.get(rowIndex);

      switch (columnIndex) {
        case 0:
          return String.valueOf(guest.getId());

        case 1:
          return guest.getName();

        case 2:
          return guest.getTaxId();
        
        case 3:
          return guest.getTelephone();

        case 4:
          return guest.getAddressLine1();

        case 5:
          return guest.getDeliveryCharge();

        case 6:
          return guest.getCurrentBalance();
      }
      return null;
    }

    public void addGuest(Guest guest) {
      int size = guestList.size();
      guestList.add(guest);
      fireTableRowsInserted(size, size);
    }

    public void deleteGuest(Guest guest, int index) {
      guestList.remove(guest);
      fireTableRowsDeleted(index, index);
    }
  }
}
